package com.open.cloud.rbac.service.permission.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.cloud.rbac.entity.permission.CorePermission;
import com.open.cloud.rbac.entity.permission.CorePermissionMenuRel;
import com.open.cloud.rbac.entity.role.CoreRolePermissionRel;
import com.open.cloud.rbac.mapper.permission.CorePermissionMapper;
import com.open.cloud.rbac.mapper.permission.CorePermissionMenuRelMapper;
import com.open.cloud.rbac.mapper.role.CoreRolePermissionRelMapper;
import com.open.cloud.rbac.object.permission.CorePermissionAllocMenuDto;
import com.open.cloud.rbac.object.permission.CorePermissionDto;
import com.open.cloud.rbac.object.permission.CorePermissionSelectDto;
import com.open.cloud.rbac.object.permission.CorePermissionVo;
import com.open.cloud.rbac.service.permission.ICorePermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Service
public class CorePermissionServiceImpl extends ServiceImpl<CorePermissionMapper, CorePermission>
        implements ICorePermissionService {

    @Resource
    private CorePermissionMapper permissionMapper;

    @Resource
    private Snowflake snowflake;

    @Resource
    private CorePermissionMenuRelMapper permissionMenuRelMapper;

    @Resource
    private CoreRolePermissionRelMapper rolePermissionRelMapper;

    /**
     * 新增权限信息
     *
     * @param permissionDto
     * @return
     */
    @Override
    public JSONObject addPermissionInfo(CorePermissionDto permissionDto) {
        CorePermission permission = new CorePermission();
        BeanUtils.copyProperties(permissionDto, permission);
        permission.setId(snowflake.nextIdStr());
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());
        int i = permissionMapper.insert(permission);
        JSONObject result = new JSONObject();
        if (i > 0) {
            result.put("code", 200);
            result.put("message", "创建成功");
        } else {
            result.put("code", 500);
            result.put("message", "创建失败");
        }
        return result;
    }

    /**
     * 更新权限信息
     *
     * @param permissionDto
     * @return
     */
    @Override
    public JSONObject updatePermissionInfo(CorePermissionDto permissionDto) {
        CorePermission permission = new CorePermission();
        BeanUtils.copyProperties(permissionDto, permission);
        permission.setUpdateTime(new Date());
        int i = this.baseMapper.updateById(permission);
        JSONObject resultObj = new JSONObject();
        if (i > 0) {
            resultObj.put("code", 200);
            resultObj.put("message", "修改成功");
        } else {
            resultObj.put("code", 500);
            resultObj.put("message", "修改失败");
        }
        return resultObj;
    }

    /**
     * 删除权限信息
     *
     * @param permissionId
     * @return
     */
    @Override
    public JSONObject deletePermissionInfo(String permissionId) {
        LambdaQueryWrapper menuRelQuery = Wrappers.<CorePermissionMenuRel>lambdaQuery()
                .eq(CorePermissionMenuRel::getPermissionId, permissionId);
        List menuRelList = permissionMenuRelMapper.selectList(menuRelQuery);

        LambdaQueryWrapper roleRelQuery = Wrappers.<CoreRolePermissionRel>lambdaQuery()
                .eq(CoreRolePermissionRel::getPermissionId, permissionId);
        List roleRelList = rolePermissionRelMapper.selectList(roleRelQuery);
        JSONObject result = new JSONObject();
        if (menuRelList.size() > 0) {
            result.put("code", 500);
            result.put("message", "该权限信息下存在关联菜单，无法删除");
        } else {
            if (roleRelList.size() > 0) {
                result.put("code", 500);
                result.put("message", "该权限信息下存在关联角色，无法删除");
            } else {
                int i = this.baseMapper.deleteById(permissionId);
                if (i > 0) {
                    result.put("code", 200);
                    result.put("message", "删除成功");
                } else {
                    result.put("code", 500);
                    result.put("message", "删除失败");
                }
            }
        }
        return result;
    }

    /**
     * 根据条件分页查询权限信息
     *
     * @param selectDto
     * @return
     */
    @Override
    public IPage<CorePermissionVo> getPermissionPage(CorePermissionSelectDto selectDto) {
        QueryWrapper<CorePermission> queryWrapper = Wrappers.<CorePermission>query()
                .orderByDesc("cp.permission_sort");
//                .eq(ObjectUtils.isNotEmpty(selectDto.getPageNo()))
        IPage<CorePermission> page = new Page<>(selectDto.getPageNo(), selectDto.getPageSize());
        IPage<CorePermissionVo> data = permissionMapper.selectPermissionByPage(page, queryWrapper);
        return data;
    }

    /**
     * 获取权限分配的菜单Id列表
     *
     * @param permissionId
     * @return
     */
    @Override
    public List<String> getPermissionRelMenu(String permissionId) {
        LambdaQueryWrapper menuRelQuery = Wrappers.<CorePermissionMenuRel>lambdaQuery()
                .eq(CorePermissionMenuRel::getPermissionId, permissionId)
                .select(CorePermissionMenuRel::getMenuId);
        List<String> menuIdList = permissionMenuRelMapper.selectList(menuRelQuery);
        return menuIdList;
    }

    /**
     * 权限分配菜单
     *
     * @param pAllocMenuDto
     * @return
     */
    @Override
    public JSONObject setPermissionAllocMenus(CorePermissionAllocMenuDto pAllocMenuDto) {
        Integer deleteCount = permissionMenuRelMapper.deletePermissionMenuRel(pAllocMenuDto.getPermissionId());
        Integer count = 0;
        if (pAllocMenuDto.getMenuIds().size() > 0) {
            ArrayList<CorePermissionMenuRel> list = new ArrayList<>();
            for (String item : pAllocMenuDto.getMenuIds()) {
                CorePermissionMenuRel pmR = new CorePermissionMenuRel();
                pmR.setId(snowflake.nextIdStr());
                pmR.setPermissionId(pAllocMenuDto.getPermissionId());
                pmR.setMenuId(item);
                pmR.setCreateTime(new Date());
                list.add(pmR);
            }
            count = permissionMenuRelMapper.batchInsertPermissionMenuRel(list);
        }
        JSONObject resultObj = new JSONObject();
        if (pAllocMenuDto.getMenuIds().size() == 0 || count > 0) {
            resultObj.put("code", 200);
            resultObj.put("message", "分配成功");
        } else {
            resultObj.put("code", 500);
            resultObj.put("message", "分配失败");
        }
        return resultObj;
    }


}
